#include "BankSystem.hh"
#include "SavingsAccount.hh"
#include "CheckingAccount.hh"
#include <iostream>
#include <sstream>
#include <cstdlib>
#include <ctime>

BankSystem::~BankSystem() {
    for (auto c : customers) delete c;
}

std::string BankSystem::generateAccountNumber(const std::string& type) {
    srand(time(0));
    std::string prefix = "62" + std::string(type == "Savings" ? "21" : "22");
    std::ostringstream oss;
    do {
        int branch = rand() % 10000;
        int num1 = rand() % 10000;
        int num2 = rand() % 10000;
        oss.str("");
        oss << prefix << "-" << branch << "-" << num1 << "-" << num2;
    } while (!isAccountNumberUnique(oss.str()));
    return oss.str();
}

bool BankSystem::isAccountNumberUnique(const std::string& number) {
    for (auto c : customers) {
        for (auto a : c->getAccounts()) {
            if (a->getAccountNumber() == number) return false;
        }
    }
    return true;
}

void BankSystem::registerCustomer(const std::string& name) {
    Customer* cust = new Customer(name);
    cust->addAccount(new SavingsAccount(generateAccountNumber("Savings"), 0));
    cust->addAccount(new CheckingAccount(generateAccountNumber("Checking"), 0));
    customers.push_back(cust);
    std::cout << "Customer " << name << " successfully registered!\n";
}

void BankSystem::displayAllAccounts() const {
    for (auto c : customers) {
        std::cout << "Customer: " << c->getName() << std::endl;
        for (auto a : c->getAccounts()) {
            std::cout << "  - " << a->getDetails() << std::endl;
        }
    }
}

void BankSystem::performTransaction() {
    // Placeholder for deposits, withdrawals, and transfers (to be implemented)
    std::cout << "Transaction functionality not yet implemented.\n";
}

void BankSystem::showReports() const {
    for (const auto& t : transactions) {
        std::cout << t.getSummary() << std::endl;
    }
}
